package net.adetech.dcweb.core.dc.rest;

import com.alibaba.fastjson.JSONObject;
import net.adetech.dcweb.core.common.entity.CommonAdminuser;
import net.adetech.dcweb.core.dc.entity.DcAuditComment;
import net.adetech.dcweb.core.dc.entity.DcInfo;
import net.adetech.dcweb.core.dc.service.IDcInfoService;
import net.adetech.utils.UtilsCommon;
import net.adetech.web.utils.UtilsUI;
import net.adetech.web.utils.UtilsWeb;
import net.adetech.web.utils.spring.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

/**
 * Created by auto on 2016/10/21.
 */
@RestController
public class DcInfoRestController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DcInfoRestController.class);

    @Autowired
    private IDcInfoService iDcInfoService;

    @PostMapping(value = "/dc/info", produces = "application/json")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        DcInfo entity = this.wrapper(DcInfo.class, request);
        entity.setState(0);
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        if (null != adminuser) {
            entity.setCreateUserId(adminuser.getId());
        }
        iDcInfoService.save(entity);
        opLog(request, logger, "增加", "");
        return UtilsUI.ok();
    }

    @GetMapping(value = "/dc/info/{id}", produces = "application/json")
    public String detail(@PathVariable Long id, HttpServletResponse response, HttpServletRequest request) {
        DcInfo entity = iDcInfoService.find(id);
        if (null == entity) {
            return null;
        }
        opLog(request, logger, "查看", "");
        return JSONObject.toJSONString(entity);
    }

    @PutMapping(value = "/dc/info/{id}", produces = "application/json")
    public String update(HttpServletRequest request, @PathVariable Long id, HttpServletResponse response) {
        DcInfo entity = this.wrapper(DcInfo.class, request);
        entity.setId(id);
        int rs = iDcInfoService.update(entity);
        opLog(request, logger, "修改", "");
        if (rs > 0) {
            return UtilsUI.ok();
        } else {
            return UtilsUI.fail();
        }

    }

    @PostMapping(value = "/dc/info/close", produces = "application/json")
    public String close(HttpServletRequest request, HttpServletResponse response) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
            DcAuditComment dcAuditComment = null;
            for (int i = 0; i < ids.length; i++) {
                dcAuditComment = new DcAuditComment();
                dcAuditComment.setAuditDate(Calendar.getInstance().getTime());
                dcAuditComment.setIsDelete(false);
                dcAuditComment.setAuditer(adminuser.getId());
                dcAuditComment.setInfoId(UtilsCommon.getLong(ids[i]));
                dcAuditComment.setRemark(UtilsWeb.getString(request,"remark"));
                dcAuditComment.setUuid(UUID.randomUUID().toString());
                dcAuditComment.setAddDate(Calendar.getInstance().getTime());
                dcAuditComment.setContent("直接取消布控");
                iDcInfoService.close(UtilsCommon.getLong(ids[i]),dcAuditComment);
            }
        }
        opLog(request, logger, "取消布控", "");
        return UtilsUI.ok();
    }


    @PostMapping(value = "/dc/info/unpass", produces = "application/json")
    public String unpass(HttpServletRequest request, HttpServletResponse response) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
            DcAuditComment dcAuditComment = null;
            for (int i = 0; i < ids.length; i++) {
                dcAuditComment = new DcAuditComment();
                dcAuditComment.setAuditDate(Calendar.getInstance().getTime());
                dcAuditComment.setIsDelete(false);
                dcAuditComment.setAuditer(adminuser.getId());
                dcAuditComment.setInfoId(UtilsCommon.getLong(ids[i]));
                dcAuditComment.setRemark(UtilsWeb.getString(request,"remark"));
                dcAuditComment.setUuid(UUID.randomUUID().toString());
                dcAuditComment.setAddDate(Calendar.getInstance().getTime());
                dcAuditComment.setContent("拒绝布控审核");
                iDcInfoService.unpass(UtilsCommon.getLong(ids[i]), dcAuditComment);
            }
        }
        opLog(request, logger, "拒绝布控审核", "");
        return UtilsUI.ok();
    }

    @PostMapping(value = "/dc/info/pass", produces = "application/json")
    public String pass(HttpServletRequest request, HttpServletResponse response) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            DcAuditComment dcAuditComment = null;
            for (int i = 0; i < ids.length; i++) {
                dcAuditComment = new DcAuditComment();
                dcAuditComment.setAuditDate(Calendar.getInstance().getTime());
                dcAuditComment.setIsDelete(false);
                if (null != adminuser) {
                    dcAuditComment.setAuditer(adminuser.getId());
                }
                dcAuditComment.setAuditType(1);
                dcAuditComment.setInfoId(UtilsCommon.getLong(ids[i]));
                dcAuditComment.setRemark("");
                dcAuditComment.setUuid(UUID.randomUUID().toString());
                dcAuditComment.setAddDate(Calendar.getInstance().getTime());
                dcAuditComment.setContent("");
                iDcInfoService.pass(UtilsCommon.getLong(ids[i]), dcAuditComment);
            }
        }
        opLog(request, logger, "同意布控审核", "");
        return UtilsUI.ok();
    }


    @DeleteMapping(value = "/dc/info", produces = "application/json")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            List
                    <Long> idsLong = new ArrayList<>(0);
            for (int i = 0; i < ids.length; i++) {
                idsLong.add(UtilsCommon.getLong(ids[i]));
            }
            iDcInfoService.delete(idsLong);
        }
        opLog(request, logger, "删除", "");
        return UtilsUI.ok();
    }

    @GetMapping(value = "/dc/infos", produces = "application/json")
    public String page(HttpServletRequest request, Integer state) {
        if (null == state) {
            return UtilsUI.fail();
        }
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        return UtilsUI.data(iDcInfoService.list(state, adminuser));
    }

    @PostMapping(value = "/dc/info/commit", produces = "application/json")
    public String commit(HttpServletRequest request) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        if (!adminuser.getRoleNo().equals("1002") && !adminuser.getRoleNo().equals("1003")) {
            return null;
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iDcInfoService.commit(UtilsCommon.getLong(ids[i]));
            }
        }
        opLog(request, logger, "提交布控审批", "");
        return UtilsUI.ok();
    }

    @PostMapping(value = "/dc/info/audit", produces = "application/json")
    public String audit(HttpServletRequest request) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            DcAuditComment dcAuditComment = null;
            Long auditer = UtilsWeb.getLong(request, "auditer");
            for (int i = 0; i < ids.length; i++) {
                dcAuditComment = new DcAuditComment();
                dcAuditComment.setAuditDate(Calendar.getInstance().getTime());
                dcAuditComment.setIsDelete(false);
                dcAuditComment.setAuditer(auditer);
                dcAuditComment.setAuditType(1);
                dcAuditComment.setInfoId(UtilsCommon.getLong(ids[i]));
                dcAuditComment.setRemark(UtilsWeb.getString(request,"remark"));
                dcAuditComment.setUuid(UUID.randomUUID().toString());
                dcAuditComment.setAddDate(Calendar.getInstance().getTime());
                dcAuditComment.setContent("提交布控审批信息");
                iDcInfoService.audit(UtilsCommon.getLong(ids[i]), dcAuditComment);
            }
        }
        opLog(request, logger, "提交布控审批", "");
        return UtilsUI.ok();
    }

    @PostMapping(value = "/dc/info/applyclose", produces = "application/json")
    public String applyclose(HttpServletRequest request) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            DcAuditComment dcAuditComment = null;
            Long auditer = UtilsWeb.getLong(request, "auditer");
            for (int i = 0; i < ids.length; i++) {
                dcAuditComment = new DcAuditComment();
                dcAuditComment.setAuditDate(Calendar.getInstance().getTime());
                dcAuditComment.setIsDelete(false);
                dcAuditComment.setAuditer(auditer);
                dcAuditComment.setAuditType(0);
                dcAuditComment.setInfoId(UtilsCommon.getLong(ids[i]));
                dcAuditComment.setRemark(UtilsWeb.getString(request,"remark"));
                dcAuditComment.setUuid(UUID.randomUUID().toString());
                dcAuditComment.setAddDate(Calendar.getInstance().getTime());
                dcAuditComment.setContent("提交取消布控审批");
                iDcInfoService.audit(UtilsCommon.getLong(ids[i]), dcAuditComment);
            }
        }
        opLog(request, logger, "提交取消布控审批", "");
        return UtilsUI.ok();
    }
}